package com.fieldchain.ex.fa100.ui.personal.activity;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.User;

import java.util.HashMap;

import com.fieldchain.ex.fa100.utils.common.VerticalUtils;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.common.EmailCode;
import com.fieldchain.ex.fa100.utils.net.common.SmsCode;
import com.fieldchain.ex.fa100.utils.net.modle.ImageRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/15
 */
public class ChangeLoginPwdActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TextView tvNametype;
    private TextView tvName;
    private EditText etOldpwd;
    private EditText etImgCode;
    private ImageView ivImgcode;
    private EditText etSmsCode;
    private TextView tvSms;
    private EditText etNewpwd;
    private EditText etRenewpwd;
    private TextView tvSure;

    private String name;
    private User user;
    private boolean isEmail = false;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        tvNametype = findViewById(R.id.tv_nametype);
        tvName = findViewById(R.id.tv_name);
        etOldpwd = findViewById(R.id.et_oldpwd);
        etImgCode = findViewById(R.id.et_imgCode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etSmsCode = findViewById(R.id.et_smsCode);
        tvSms = findViewById(R.id.tv_sms);
        etNewpwd = findViewById(R.id.et_newpwd);
        etRenewpwd = findViewById(R.id.et_renewpwd);
        tvSure = findViewById(R.id.tv_sure);
    }

    @Override
    public void setValue() {
        getImage();
        user = ShareManager.getInstance().getUser();
        if (user.getPhone() != null && user.getPhone().length() > 0) {
            name = user.getPhone();
            isEmail = false;
        } else {
            tvNametype.setText("邮箱");
            name = user.getEmail();
            isEmail = true;
        }
        tvName.setText(name);
        tvCenter.setText("修改登录密码");
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        ivImgcode.setOnClickListener(this);
        tvSms.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_imgcode:
                getImage();
                break;
            case R.id.tv_sms:
                VerticalUtils.getInstance().start(tvSms);
                if (!isEmail) {
                    SmsCode.getInstance().getSms(tvName.getText().toString(), etImgCode.getText().toString());
                } else {
                    EmailCode.getInstance().getEmail(tvName.getText().toString(), etImgCode.getText().toString());
                }
                break;
            case R.id.tv_sure:
                change();
                break;
            case R.id.rea_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VerticalUtils.getInstance().cancle();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_changeloginpwd;
    }

    public void getImage() {
        HttpRequest.getInstance().getImage(Constant.image, new ImageRequest() {
            @Override
            public void imgReceive(Bitmap bit) {
                ivImgcode.setImageBitmap(bit);
            }
        });
    }

    public void change() {
        String oldPws=etOldpwd.getText().toString();
        String newPws=etNewpwd.getText().toString();
        String rNewPws=etRenewpwd.getText().toString();
        String smsCode=etSmsCode.getText().toString();

        if(!newPws.equals(rNewPws)){
            MyToast.show("两次输入不一致");
            return;
        }

        HashMap map=new HashMap();
        map.put("oldPw",oldPws);
        map.put("smsCode",smsCode);
        map.put("newPw",newPws);
        HttpRequest.getInstance().postRequest(map, Constant.changeLoginPwd,new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
              MyToast.show(msg);
              if(code==200){
                  finish();
              }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
